local Sol = LibStub("Sol");
local nkLibChannels = LibStub("nkLibChannels");

nkPartyQuest.Com = {};

local FloodDelay = 5;

nkPartyQuest.Com.DataPrefix = {	["SENDALLDATA"] = "NKPQDATA";
								["SENDNEWQUEST"] = "NKPQNEW";
								["SENDUPDATE"] = "NKPQUPD";
								["SENDCANCEL"] = "NKPQDEL"
								}
nkPartyQuest.Com.ChannelName = nil;
nkPartyQuest.Com.ChannelNamePrefix = "NKPQCHP";
nkPartyQuest.Com.TransferData = nil;

nkPartyQuest.Com.UpdateTransData = function (UpdateTable)
	
	for k in pairs (UpdateTable) do	
		--DEFAULT_CHAT_FRAME:AddMessage ("adding quest " .. k .. " to delayed sending");
		if nkPartyQuest.Com.TransferData == nil then
			nkPartyQuest.Com.TransferData = {};
		end;
		
		nkPartyQuest.Com.TransferData[k] = UpdateTable[k];
	end;
end;

-- Send quest information to other players
-- Input:	UpdateTable	- Table containing all the updates
-- Output:	None
nkPartyQuest.Com.SendQuestData = function ()

	local AllDataSent = true;

	if nkPartyQuest.Com.TransferData == nil then
		return;
	end;

	local datastream = "";
	local ChannelNo = nkLibChannels.GetChannelNo(nkPartyQuest.Com.ChannelName);
	
	for k in pairs(nkPartyQuest.Com.TransferData) do
		
		local tmpData = k .. ":" .. tostring(nkPartyQuest.Com.TransferData[k].status);
			
		if #(datastream .. tmpData) > 200 then
			-- Addin latest data record would make datastream too large, rest will be send next update cycle
			AllDataSent = false;
			break;
		elseif (#datastream > 0) then 
			datastream = datastream .. ";";
		end;		

		datastream = datastream .. k .. ":" .. tostring(nkPartyQuest.Com.TransferData[k].status);
		nkPartyQuest.Com.TransferData[k] = nil;
	end;
	
	if datastream ~= "" then
		-- Had the effect of sometimes the quest quest not be regarded as empty. This is to prevent an empty command to be send
		SendChatMessage(nkPartyQuest.Com.DataPrefix.SENDALLDATA .. "-" .. datastream, "CHANNEL", 0, ChannelNo) ;
	end;
	
	if AllDataSent == true then
		--DEFAULT_CHAT_FRAME:AddMessage("setting data to nil");
		nkPartyQuest.Com.TransferData = nil;
	end;
	
end;

-- Processes information from other players
-- Input:	playername	- The name of the player who sent the data
--			datastream	- The data being sent
nkPartyQuest.Com.ProcessDataStream = function (playername, datastream)

	local playerID = nil;
	local NewPlayerInfo = false;
	local MatchingQuest = false;
	local UpdateType = nil;  -- 0 = all data, 1 = new quest, 2 = update, 3 = cancelled quest
	
	--DEFAULT_CHAT_FRAME:AddMessage("Caputered data from " .. playername);
	
	nkPartyQuest.Debug = datastream;
	
	if nkPartyQuest.Core.QuestList[playername] == nil then
		nkPartyQuest.Core.QuestList[playername] = {};
		NewPlayerInfo = true;
	end;
	
	nkPartyQuest.LastDataStream = datastream;
	local nStart = nil;
	local nEnd = nil; 

	nStart, nEnd = string.find (datastream, nkPartyQuest.Com.DataPrefix.SENDALLDATA);
	UpdateType = 0;
	
	if (nStart == nil) then
		--DEFAULT_CHAT_FRAME:AddMessage("couldn't find start of data in stream");
		return;
	end;
	
	datastream = string.sub (datastream, nEnd+2);
	questdata = Sol.string.Split (datastream, ";");
	
	--DEFAULT_CHAT_FRAME:AddMessage("----------->" .. #questdata );
	
	for idx = 1, #questdata do
		if questdata[idx] == nil or questdata[idx] == "" then break; end;
		
		local questinfo = Sol.string.Split(questdata[idx], ":");
		local catalogID = questinfo[1];		
		local shortname = questinfo[2];
		local level = questinfo[3];
		local exp = questinfo[4];
		local status = 1;
		
		if #questinfo >= 5 then
			status = tonumber(questinfo[5]);
		end;
				
		local ident = catalogID .. ":" .. shortname .. ":" .. level .. ":" .. exp;
		
		if (nkPartyQuest.Core.QuestList[UnitName("player")] ~= nil) then
			if (nkPartyQuest.Core.QuestList[UnitName("player")] [ident] ~= nil) then MatchingQuest = true; end;
		end; 
		
		if status == 0 then
			nkPartyQuest.Core.QuestList[playername][ident] = nil;
		else			
			nkPartyQuest.Core.QuestList[playername][ident] = {
				ident = ident;
				catalogID = catalogID;
				shortname = shortname;
				level = level;
				exp = exp;
				status = status};
		end;		
	end;
	
	if MatchingQuest == true and NewPlayerInfo == true then
		-- return full quest information if the data was received from a new player and a matching quest has been found		 
		nkPartyQuest.Com.UpdateTransData(nkPartyQuest.Core.QuestList[playername]); 
	end;
end;

nkPartyQuest.Com.DailyChannel = function ()

	local DailyChannelExists = false;

	nkPartyQuest.Com.ChannelName = nkPartyQuest.Com.ChannelNamePrefix .. os.date("%m%d");
	
	local ChannelList = { GetChannelList() };
	
	if #ChannelList == 0 then return false; end;
	
	--DEFAULT_CHAT_FRAME:AddMessage(#ChannelList);
	
	for idx = 1, #ChannelList, 2 do
		--DEFAULT_CHAT_FRAME:AddMessage(ChannelList[idx+1]);
		if string.find (ChannelList[idx+1], nkPartyQuest.Com.ChannelNamePrefix) then
			if ChannelList[idx+1] ~= nkPartyQuest.Com.ChannelName then
				--DEFAULT_CHAT_FRAME:AddMessage ("leaving channel");
				LeaveChannel (ChannelList[idx+1])
			else
				DailyChannelExists = true;
			end;
		end;
	end;
	
	return DailyChannelExists; 
	
end;